package com.kawins.supers.table.core;

import com.kawins.base.BaseService;
import com.kawins.result.ResultTo;
import com.kawins.supers.log.AbsLogger;
import com.kawins.supers.table.DataTablePage;
import tk.mybatis.mapper.entity.Example;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Map;

/**
 * @author rankai
 *         createTime 2017-09-2017/9/26 14:49
 */
public abstract class TableAdapter<T> extends AbsLogger {

    protected abstract BaseService buildService();

    /**
     * 分页Base方法
     *
     * @return
     */
    protected ResultTo dataPage() {
        return build().pageList();
    }

    /**
     * 分页Base方法,排除exception中的搜索Key
     *
     * @param exception
     * @return
     */
    protected ResultTo dataPage(String... exception) {
        return build().pageList(exception);
    }

    /**
     * 增加map参数
     *
     * @param parameterMap
     * @return
     */
    protected ResultTo dataPage(Map<String, Object> parameterMap) {
        return build().pageList(parameterMap);
    }

    /**
     * 增加map参数,排除exception中的搜索Key
     *
     * @param parameterMap
     * @param exception
     * @return
     */
    protected ResultTo dataPage(Map<String, Object> parameterMap, String[] exception) {
        return build().pageList(parameterMap, exception);
    }

    /**
     * 自定义Example对象
     *
     * @param example
     * @return
     */
    protected ResultTo dataPage(Example example) {
        return build().pageList(example);
    }

    /**
     * 自定义Example对象,增加map参数
     *
     * @param example
     * @param parameterMap
     * @return
     */
    protected ResultTo dataPage(Example example, Map<String, Object> parameterMap) {
        return build().pageList(example, parameterMap);
    }

    /**
     * 自定义Example对象,增加map参数,排除exception中的搜索Key
     *
     * @param example
     * @param parameterMap
     * @param exception
     * @return
     */
    protected ResultTo dataPage(Example example, Map<String, Object> parameterMap, String[] exception) {
        return build().pageList(example, parameterMap, exception);
    }

    /**
     * 自定义分页service,无自定义参数
     *
     * @param methodName
     * @return
     */
    protected ResultTo dataPage(String methodName) {
        try {
            return build().pageList(methodName);
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 自定义分页service,带自定义参数
     *
     * @param methodName
     * @param parameterMap
     * @return
     */
    protected ResultTo dataPage(String methodName, Map<String, Object> parameterMap) {
        try {
            return build().pageList(methodName, parameterMap);
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @param baseService
     * @param clazz
     * @param methodName
     * @return
     */
    protected ResultTo dataPage(BaseService baseService, Class clazz, String methodName) {
        try {
            return new DataTablePage(baseService, clazz).pageList(methodName);
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @param baseService
     * @param clazz
     * @param methodName
     * @param parameterMap
     * @return
     */
    protected ResultTo dataPage(BaseService baseService, Class clazz, String methodName, Map<String, Object> parameterMap) {
        try {
            return new DataTablePage(baseService, clazz).pageList(methodName, parameterMap);
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    private DataTablePage build() {
        return new DataTablePage(buildService(), getEntryClass());
    }

    private Class<T> getEntryClass() {
        Class<T> entityClass = null;
        Type genericSuperclass = getClass().getGenericSuperclass();
        if (genericSuperclass instanceof ParameterizedType) {
            Type[] types = ((ParameterizedType) genericSuperclass).getActualTypeArguments();
            entityClass = (Class<T>) types[0];
        }
        return entityClass;
    }
}
